#import "wooddoc.h"

@implementation WoodDoc (TreeDelegate)

static BOOL			shouldAlertTextSave = YES;

- updateViewsDirty:(BOOL)aBool rect:(NXRect *)aRect flag:(int)aFlag
{
	BOOL resized;
	NXRect scrollRect;
	
	[window disableFlushWindow];
	if(aFlag & CHECK_RESIZE){
		resized = [treeView checkResize:NULL for:tree];
		if(resized){
			[scrollView tile]; 
			[scrollView display];
		}
	}
	if(aFlag & UPDATE_TREEVIEW)
		if(aRect)
			[treeView display:aRect :1];
		else 
			[treeView display];
	if(aFlag & UPDATE_TEXTVIEW)
		[textView display];
	if((aFlag & SCROLL_TREEVIEW) && lastSelectedNode){
		[lastSelectedNode getBounds:&scrollRect];
		[treeView scrollRectToVisible:&scrollRect];
	}
	if(aBool)
		[self dirty:YES];
	[window reenableFlushWindow];
	[window flushWindow];
	[window makeFirstResponder:treeView];
	return self;
}

- tree
{
	return tree;
}

- declareSelection:aNode
{
	id retVal;
	id text;
	int doIt;

	retVal = lastSelectedNode;
	if(lastSelectedNode != aNode){
		if(shouldAlertTextSave && !textViewSaved && lastSelectedNode){
			doIt = NXRunAlertPanel("Node Description", "Save ?", "Yes", "No", NULL);
			if(doIt == NX_ALERTDEFAULT)
				[self changeDescription:self];
			textViewSaved = YES;
		}
		lastSelectedNode = aNode;
		text = [textView docView];
		[text selectAll:self];
		if(aNode){
			[aNode putDescriptionIn:text];
			[[NXApp inspectorManager] inspect:aNode];
			[aNode setSelected:YES];
		} else {
			[text delete:self];
			[[NXApp inspectorManager] inspect:nil];
		}
		[text selectNull];
		if(retVal)
			[retVal setSelected:NO]; 
	}
	return retVal;
}

- (float)docScale
{
	return docScale;
}

- setDocScale:(float)aScale
{
	docScale = aScale;
	return self;
}

- currentNode	
{
	return lastSelectedNode;
}

- (BOOL)showMarker
{
	return showMarker;
}

- undoManager
{
	return undoManager;
}

@end
